package com.lianqi.emcpframework.shiro.cas.realm;

import com.lianqi.emcpframework.shiro.cas.util.JwtTokenUtils;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.jwt.profile.JwtGenerator;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @program emcp-framework
 * @ClassName JwtTokenSupport
 * @description: jwt token生成
 * @author: sky
 * @create: 2019/05/31 18:01
 */
public class JwtTokenSupport implements AuthenticateListener{

    @Autowired
    DefaultWebSessionManager sessionManager;

    @Autowired
    JwtGenerator generator;

    public static final String CAS_CLIENT_NAME = "cas";

    @Override
    public void onAuthenticate(CommonProfile profile) {
        String clientName = profile.getClientName();
        //页面登录时生成jwt token
        if (CAS_CLIENT_NAME.equals(clientName)) {
            String jwtToken = JwtTokenUtils.provideJwtToken(profile, generator,
                    sessionManager.getGlobalSessionTimeout());
            profile.addAttribute(JwtTokenUtils.JWT_TOKEN, jwtToken);
        }
    }
}
